package com.example.liwushuo.fragment;

import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSONObject;
import com.example.liwushuo.BaseApp;
import com.example.liwushuo.GoodsInfoActivity;
import com.example.liwushuo.R;
import com.example.liwushuo.adapter.GridViewAdapter;
import com.example.liwushuo.bar.OneBar;
import com.example.liwushuo.bean.BaseBean;
import com.example.liwushuo.bean.Item;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.bitmap.PauseOnScrollListener;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author 时伟超 这是单品的碎片
 */
public class OneProductFragment extends Fragment implements
		OnRefreshListener2<GridView>, OnItemClickListener {

	private String url;
	private PullToRefreshGridView gridView;
	private GridViewAdapter adapter;
	private List<Item> list;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		url = getArguments().getString("url");
		Log.i("url", "----------" + url);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.one_product_fragment, null);
		// 添加标题栏
		getFragmentManager().beginTransaction()
				.replace(R.id.one_bar_id, new OneBar()).commit();
		initView(view);
		initData();
		return view;
	}

	private void initView(View view) {
		gridView = (PullToRefreshGridView) view.findViewById(R.id.gridVeiw);
		list = new ArrayList<Item>();
		adapter = new GridViewAdapter(getActivity(), list);
		// 设置当滚动式不加载图片
		gridView.setOnScrollListener(new PauseOnScrollListener(BaseApp
				.getBitmapUtils(), true, false));

		GridView gridView2 = gridView.getRefreshableView();
		addHandView(gridView2);
		gridView.setOnItemClickListener(this);
		gridView.setAdapter(adapter);

	}

	private void addHandView(GridView gridView2) {

		TextView tv = new TextView(getActivity());
		tv.setText("个人能够吸纳时间");

	}

	private void initData() {
		HttpUtils httpUtils = new HttpUtils();
		// Log.i("url", "-------------"+url);
		httpUtils.send(HttpMethod.GET, url, new RequestCallBack<String>() {
			@Override
			public void onFailure(HttpException arg0, String arg1) {

				Toast.makeText(getActivity(), "请检查网络", Toast.LENGTH_SHORT)
						.show();
			}

			@Override
			public void onSuccess(ResponseInfo<String> response) {
				String json = response.result;

				// 解析数据
				if (json != null) {
					BaseBean bean = JSONObject
							.parseObject(json, BaseBean.class);

					if (bean.getMessage().equals("OK")) {

						list.addAll(bean.getData().getItems());
						adapter.notifyDataSetChanged();
						Log.i("bean", "--------------" + list.size());

					}

				}
			}
		});

	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {

	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {

		Item item = list.get(position);
		Intent intent = new Intent(getActivity(), GoodsInfoActivity.class);
		intent.putExtra("item", item);
		startActivity(intent);

	}
}
